package leetcode_top;


import linked.ListNode;

public class Ex328 {
    /**
     * Definition for singly-linked list. public class ListNode { int val; ListNode
     * next; ListNode() {} ListNode(int val) { this.val = val; } ListNode(int val,
     * ListNode next) { this.val = val; this.next = next; } }
     */
    class Solution {
        public ListNode oddEvenList(ListNode head) {
            if (head == null || head.next == null) return head;
            ListNode pre = head.next, post = head, h1 = post, h2 = pre, tmp;

            while (pre != null && pre.next != null) {
                tmp = pre.next;
                pre.next = tmp.next;
                post.next = tmp;
                post = post.next;
                pre = pre.next;
            }
            post.next = h2;
            return h1;
        }
    }
}
